package offer.easy;

public class ReplaceSpace {

    public static String replaceSpaceByCode(String str) {
        if (str == null || "".equals(str)) {
            return str;
        }

        String res = "";
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                res += "%20";
            } else {
                res += str.charAt(i);
            }
        }

        return res;
    }

    public static String replaceFunction(String str) {
        if (str == null || "".equals(str)) {
            return str;
        }
        str = str.replaceAll(" ","%20");
        return str;
    }

    public static void main(String[] args) {
        String str = "We are the sword!";
        System.out.println(replaceSpaceByCode(str));
        System.out.println(replaceFunction(str));
    }

}
